/**
 * 客户设备档案信息维护
 * @author miaoyl
 * @date 2018/01/16
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_where = {},
        cols = [[
            {type:'checkbox',fixed: 'left'},
            {field: 'MMRA001', width: 80, title: '客户编号',search:'default',searchOpts:{icon:true}}
            , {field: 'MMRA044', width: 80, title: '项目编号',hide:true}
            , {field: 'MMRA045', width: 80, title: '合同编号'}
            , {field: 'MMRA002', width: 100, title: '设备编号'}
            , {field: 'MMRA004', width: 140, title: '品名'}
            , {field: 'MMRA006', width: 100, title: '规格'}
            , {field: 'MMRA003', width: 100, title: '产品序列号'}
            , {field: 'MMRA009', width: 80, title: '销售单号'}
            , {field: 'MMRA010', width: 100, title: '销售日期'}
            , {field: 'MMRA011', width: 100, title: '安装日期'}
            , {field: 'MMRA008', width: 100, title: '制造日期'}
            , {field: 'MMRA013', width: 100, title: '是否有保修',filterDatas: app.datas_('sys_1_0')}
            , {field: 'MMRA012', width: 140, title: '三包到期日期'}
            , {field: 'MMRA017_NAME', width: 100, title: '设备销售方'}
            , {field: 'MMRA018', width: 140, title: '设备制造厂商'}
            , {field: 'MMRA019', width: 80, title: '照片地址'}
            , {field: 'MMRA042_NAME', width: 100, title: '业务员姓名'}
            , {field: 'MMRA043', width: 100, title: '业务员手机号'}
            , {field: 'MMRA020', width: 100, title: '负责经销商'}
            , {field: 'MMRA021', width: 150, title: '经销商销售员微信号'}
            , {field: 'MMRA016', width: 60, title: '备注'}
            , {field: 'MMRAACTI', width: 60, title: '有效否',filterDatas: app.datas_('sys_y_n')},
            {
                width: 60,
                title: '',
                toolbar: '#script-gwai610setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwai610SettingAction
    ;

    var Gwai610 = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai610',
                _searchs: [{
                    id: 'gwai610', tableId: 'gwai610_table', datas: cols[0]
                }],
                _loadTplsFull: [
                    {dom: 'gwai610_edit', tpl: 'gwai610_edit'},
                    {dom: 'common_open_window',tpl: 'common/_open_window'}
                 ]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwai610_table'),
                    url: g_ctxPath + 'crm/gwai610/getMmralist',
                    exportsUrl: g_ctxPath + 'crm/gwai610/getMmralist',
                    exportsName: '客户设备档案数据',
                    title: '客户设备档案信息表',
                    method: 'post',
                    id: 'gwai610_table',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where:table_where,
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwai610setting-action').innerHTML).render({}, function (string) {
                    gwai610SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwai610_table)', function (obj) {

                    layer.tips(gwai610SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwai610_edit',
                                            tagData: obj,
                                            index: 0,
                                            openOpts:{
                                                title: '客户设备档案信息编辑',
                                                maxmin: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwai610_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwai610_edit',
                        tagData: obj,
                        index: 0,
                        openOpts: {
                            title: '客户设备档案信息编辑',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwai610_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwai610_edit',
                                tagData: {
                                    is_add: true
                                },
                                index: 0,
                                openOpts: {
                                    title: '客户设备档案信息编辑',
                                    maxmin: true
                                }
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.delDataBatch();
                                }
                            });
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwai610_table', where: table_where});
        },

        delData: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwai610/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: '客户设备档案信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        delDataBatch: function(){
            var checkStatus = table.checkStatus('gwai610_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('crm_gwai610');
                    _temp.MMRA001 = v.MMRA001;
                    _temp.MMRA002 = v.MMRA002;
                    _temp.MMRA003 = v.MMRA003;
                    _temp.MMRAENT = v.MMRAENT;
                    _xdata.push(_temp);
                });
                app.ajax({
                    xurl: 'crm/gwai610/delDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: '客户设备档案信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwai610',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'MMRA001') {//客户开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.MMRA001 = data.GWAK001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '430px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new Gwai610();

});
